<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Paginator;
use app\index\controller\Log\InsertLog as IL;

/**
 * 产品评论管理
 */
class Review extends Controller
{
    /**
     * 2018-4-16  日期格式
     * 曹振威 开发人
     * 获取评论的子集
     * @param type $review_id
     * @param type $where
     * @return type
     */
    public function get_review($review_id,$where,$field){
        $child_review = Db::table('review')->alias('r')
                ->join('product p','r.product_id=p.id','LEFT')
                ->join('systematics s','p.systematics_id=s.id','LEFT')
                ->where($where)
                ->where('r.superior_id',$review_id)
                ->field($field)
                ->select();
        $arr = array();
        foreach ($child_review as $key => $val ) {
            $child = $this->get_review($val["id"],$where,$field);
            if($child){                
                $val['child_review'] = $child;
            }
            $arr[] = $val; 
        }
        return $arr;
    }

    /**
     * 2018-4-16 日期格式
     * 曹振威 开发人
     * 评论列表
     */
    public function review_list() {
        $user_id = $this->user_id;
        $system_id = $this->system_id;
        $key_word = input('key_word');
        if($key_word){
            $where['r.commentary'] = ['LIKE','%'.$key_word.'%'];
        }
        $where['r.business_id'] = $this->business_id;
        $where['r.store_id'] = $this->store_id;
        $where['r.system_id'] = $system_id;
        $where['r.state'] = 0;
        $field = 'r.id,r.system_id,r.user_id,r.user_name,r.commentary,r.createtime,r.review_level,r.review_img,r.order_sn,r.createtime,r.superior_id,p.product_name,s.title';
        $list_page = Db::table('review')->alias('r')->where($where)->where('r.superior_id',0)->paginate()->toArray();
        $list['total'] = $list_page['total'];
        $list['per_page'] = $list_page['per_page'];
        $list['current_page'] = $list_page['current_page'];
        $list['last_page'] = $list_page['last_page'];
        $list_data = $this->get_review(0,$where,$field);
        $list['data'] = $list_data;
        
        
        $date = array('code'=>666,'msg'=>'评论获取成功','data'=>$list);
        IL::insert('log', [
            'user_id'=>$this->user_id,
            'operation_user'=> $this->user['name'],
            'operation_name'=>'查看', 
            'operation_content'=>'查看评论列表',
            'ip'=> Request()->ip(),
            'createtime'=>date('Y-m-d H:i:s'),
            'state'=>1
        ]);
        return $date;
    }
    
    /**
     * 2018-4-13 日期格式
     * 曹振威 开发人
     * 评论详情
     * @return type
     */
    public function review_info() {
        $product = array();
        $user_id = $this->user_id;
        $system_id = input('system_id');
        $review_id = input('post.id');
        if(!$system_id || !$this->business_id || !$review_id){
            $date = array('code'=>20021,'msg'=>'非法操作','data'=>'');
            exit(json_encode($date));
        }
        $where['business_id'] = $this->business_id;
        $where['system_id'] = $system_id;
        $where['state'] = 0;
        $field = 'id,system_id,user_id,user_name,commentary,createtime,review_level,review_img,order_sn,createtime,superior_id';
        $list = $this->get_review(0,$where,$field);
        foreach ($list as $key => $value) {
            $order_sn = $value['order_sn'];
            $fileds = 'id,system_id,business_id,batchcode,pay_batchode,product_id,price,quantity,createtime';
            $list['product'][] = Db::table('orders_info')->where('batchcode',$order_sn)->field($fileds)->select();
        }
        if($list){
            $date = array('code'=>666,'msg'=>'详情获取成功','data'=>$list);
        } else {
            $date = array('code'=>20033,'msg'=>'详情获取失败','data'=>'');
        }
        
        return $date;
    }
    
    /**
     * 2018-04-27 开发日期
     * 曹振威 开发人
     * 删除评论
     * @return string
     */
    public function review_del() {
        $user_id = $this->user_id;
        $system_id = $this->system_id;
        $ids = input('id');
        if(empty($ids)){
            $this->check_date = 1;
            $date = array('code'=>20021,'msg'=>'非法操作','data'=>'');
        }
        $review_ids = explode(",",$ids);
        $review = Db::table('review')->where(array('business_id'=>$this->business_id,'id'=>['IN',$review_ids],'state'=>0))->select();
        if(!$review){
            $date = array('code'=>20069,'msg'=>'不存在该评论','data'=>'');
            $this->check_date = 1;
        }
        if($this->check_date != 0){
            exit(json_encode($date));
        }
        $row = Db::table("review")->where(["business_id"=>$this->business_id])->whereIn('id',$review_ids)->update(['state'=>1,'createtime' => date('Y-m-d H:i:s')]);  //商品评论
        if($row){
            $date = array('code'=>666,'msg'=>'评论删除成功','data'=>'');
            IL::insert('log', [
                'user_id'=>$this->user_id,
                'operation_user'=> $this->user['name'],
                'operation_name'=>'删除', 
                'operation_content'=>'删除评论',
                'ip'=> Request()->ip(),
                'createtime'=>date('Y-m-d H:i:s'),
                'state'=>1
            ]);
        } else {
            $date = array('code'=>20049,'msg'=>'评论删除失败','data'=>'');
        }
        return $date;
    }
}
